<script lang="ts" setup>
import { onMounted, ref, watch } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { reqGetAppPageList } from '@/api/square.ts'
import type {
  queryAppParams,
  respAppData,
  respConfigKeysData,
} from '@/pages/member/api/types/application'
import { reqGetConfigContent,reqPreApplicationRun } from '@/pages/member/api/application.ts'
import { MsgError, MsgSuccess } from '@/utils/utils.ts'
const router = useRouter() // 路由

const toDetail = async (item: respAppData) => {
  const resp = await reqPreApplicationRun(item.id)
  if (resp.code === 0) {
    // MsgSuccess('环境创建成功')
    router.push({
      path: '/square/detail',
      query: {
        appId: item.id,
        runId: resp.data.runId,
        applicationId: resp.data.applicationId,
        smartFlowId: resp.data.smartFlowId,
        title: item.title,
        handlerType: 'read',
      },
    })
  } else {
    // MsgError('环境创建失败')
  }
}

onMounted(() => getApplicationType())

const typeValue = ref<number>(0)
const typeTabs = ref<respConfigKeysData[]>([])
const getApplicationType = async () => {
  const { data } = await reqGetConfigContent('applicationType')
  typeTabs.value = data as respConfigKeysData[]
  typeValue.value = ''
  subTypeTabs.value = data[0].sub
  subTypeValue.value = ''
}

const onChangeTab = (tab) => {
  typeValue.value = tab.key
  subTypeValue.value = ''
}

const subTypeValue = ref<number | string>(0)
const subTypeTabs = ref<respConfigKeysData[]>()
const onChangeSubType = (subType: respConfigKeysData) => {
  subTypeValue.value = subType.key
}

const total = ref(0)
const pageNo = ref(1)
const onCurrentChange = (page: number) => {
  pageNo.value = page
}

watch([typeValue, subTypeValue, pageNo], () => appPageList())

const keyword = ref('')
onMounted(() => appPageList())
const appList = ref<respAppData[]>([])
const appPageList = async () => {
  const reqData: queryAppParams = {
    pageNo: pageNo.value,
    title: keyword.value,
    type: typeValue.value || '',
    subType: subTypeValue.value || '',
  }
  let resp = await reqGetAppPageList(reqData)
  if (resp.code === 0) {
    appList.value = resp.data.list
    total.value = resp.data.total
  }
}
</script>

<template>
  <div class="app-market-container">
    <div class="common-banner">
      <img src="@/assets/images/square/app-market-banner.png" alt="" />
      <div class="content">
        <div class="common-w-box">
          <div class="txt-c">
            <div class="t1">应用广场</div>
            <div class="search-c">
              <el-input
                v-model="keyword"
                placeholder="请输入需要搜索的内容"
                class="input-with-select"
                size="large"
              >
                <template #prepend>
                  <el-select
                    v-model="subTypeValue"
                    placeholder="请选择分类"
                    style="width: 130px"
                    size="large"
                  >
                    <el-option label="全部分类" value="" />
                    <el-option
                      :label="item.value"
                      :value="item.key"
                      v-for="item in subTypeTabs"
                      :key="item.key"
                    />
                  </el-select>
                </template>
                <template #append>
                  <el-button
                    type="primary"
                    link
                    class="search-btn"
                    @click="appPageList()"
                  >
                    搜索
                  </el-button>
                </template>
              </el-input>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="common-w-box">
      <div class="cnb" style="min-height: 500px">
        <div class="common-slider">
          <div class="title" :class="{ on: subTypeValue === '' }">全部分类</div>
          <div class="menu-box">
            <ul>
              <li
                :class="{ on: subTypeValue === item.key }"
                v-for="item in subTypeTabs"
                :key="item.key"
                @click="onChangeSubType(item)"
              >
                {{ item.value }}
                <el-icon class="el-icon--right" v-if="item.value === 0">
                  <ArrowRight />
                </el-icon>
              </li>
            </ul>
          </div>
        </div>
        <div class="cn-list">
          <div class="common-tabs" style="margin-bottom: 20px">
            <span
              v-for="item in typeTabs"
              :key="item.key"
              :class="{ on: typeValue === item.key }"
              @click="onChangeTab(item)"
            >
              {{ item.value }}
            </span>
          </div>
          <div>
            <el-row :gutter="24" class="common-lists">
              <template v-if="appList.length < 1">
                <el-empty style="width: 100%" description="无数据记录" />
              </template>
              <el-col :span="8" v-else v-for="item in appList" :key="item.id" @click="toDetail(item)">
                <div class="item cursor hover-shadow">
                  <div class="img">
                    <img :src="item.image" alt="" />
                  </div>
                  <div class="txt-c">
                    <div class="name">{{ item.title }}</div>
                    <div class="tag">{{ item.titleEn }}</div>
                    <div class="des">
                      {{ item.description }}
                    </div>
                  </div>
                </div>
              </el-col>
            </el-row>
            <el-row class="flex-xbt">
              <i></i>
              <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                @current-change="onCurrentChange"
              />
            </el-row>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.common-lists .item .img {
  overflow: hidden;
  img {
    width: 100%;
  }
}
.app-market-container {
  .cnb {
    display: flex;
    padding: 24px 0;

    .cn-list {
      flex: 1;
      padding-left: 16px;

      :deep(.el-col) {
        list-style: none;
      }
    }
  }

  .common-lists {
    min-height: 248px;
  }
}
</style>
